package com.aps.service.impl;

import com.aps.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.aps.entity.ApsExeption;
import com.aps.entity.domain.dish.DishInterval;
import com.aps.entity.eunm.ResultType;
import com.aps.page.BaseQuery;
import com.aps.service.IDishIntervalService;
import com.aps.service.rep.DishIntervalReq;

/**
 * @author liaoguangan
 * @description <>
 * @date 2018/4/9 10:10
 */

@Service
public class DishIntervalServiceImpl implements IDishIntervalService {

    @Autowired
    private DishIntervalReq dishIntervalReq;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询列表
     *
     * @param baseQuery
     */
    @Override
    public Pagination<DishInterval> list(BaseQuery baseQuery) {

        String sql = " select * from aps_dish_interval adi ";
        return new Pagination<DishInterval>().search(jdbcTemplate, DishInterval.class, sql,
                new Object[] {}, baseQuery, "adi");

    }

    /**
     * 删除查询列表
     *
     * @param id
     */
    @Override
    public void remove(String id) {
        DishInterval di = dishIntervalReq.findOne(id);
        if (null == di) {
            throw new ApsExeption(ResultType.RECORD_NOT_EXIST);
        }

        di.setIsDeleted(true);
        dishIntervalReq.save(di);
    }

    /**
     * 增加
     *
     * @param dishInterval
     */
    @Override
    public DishInterval add(DishInterval dishInterval) {

        DishInterval di = dishIntervalReq.findByPreDishCodeAndNextDishCode(
                dishInterval.getPreDishCode(), dishInterval.getNextDishCode());
        if (null != di) {
            throw new ApsExeption(ResultType.RECORD_ALWAYS_EXIST);
        }
        return dishIntervalReq.save(dishInterval);
    }

    /**
     * 更新
     *
     * @param dishInterval
     * @return
     */
    @Override
    public DishInterval update(DishInterval dishInterval) {
        DishInterval di = dishIntervalReq.findOne(dishInterval.getId());
        if (null == di) {
            throw new ApsExeption(ResultType.RECORD_NOT_EXIST);
        }
        di.setConstraintTime(dishInterval.getConstraintTime());
        di.setConstraintTimeUnit(dishInterval.getConstraintTimeUnit());
        return dishIntervalReq.save(di);
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Override
    public DishInterval detail(String id) {
        DishInterval di = dishIntervalReq.findOne(id);
        if (null == di) {
            throw new ApsExeption(ResultType.RECORD_NOT_EXIST);
        }

        return di;
    }
}
